package org.example.mybatis.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.example.mybatis.domain.AiSession;
import org.example.mybatis.domain.PageDomain;
import org.example.mybatis.mapper.AiSessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Page {

    @Autowired
    private AiSessionMapper aiSessionMapper;


    public IPage<AiSession> getPage(PageDomain pageDomain) {
        // 限制最大页大小（避免一次查询过多数据）
        int pageSize = Math.min(pageDomain.getPageSize(),  50);
        // 页码不能小于1
        int pageNum = Math.max(pageDomain.getPageNum(), 1);

        LambdaQueryWrapper<AiSession> queryWrapper = new LambdaQueryWrapper<AiSession>()
                .orderByDesc(AiSession::getUpdatedAt); // 按更新时间倒序（最新的在前）

        // 分页查询
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<AiSession> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNum, pageSize);
        return aiSessionMapper.selectPage(page, queryWrapper);
    }
}
